<template>
  <el-card>
    <el-table :data="tableData" border style="margin: 10px 0px">
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column
        prop="skuName"
        label="名称"
        show-overflow-tooltip
        width="150px"
      />
      <el-table-column
        prop="skuDesc"
        label="描述"
        show-overflow-tooltip
        width="150px"
      />
      <el-table-column label="默认图片" width="150px">
        <template #="{ row }">
          <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量(g)" width="150px" />
      <el-table-column prop="price" label="价格(元)" width="150px" />
      <el-table-column prop="address" label="操作" width="220px" fixed="right">
        <template #="{ row, index }">
          <el-button
            :type="row.isSale == 1 ? 'info' : 'success'"
            size="small"
            :icon="row.isSale == 1 ? 'Bottom' : 'Top'"
            @click="SaleOrCancel(row)"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="Edit"
            @click="udateAttr(row)"
          ></el-button>
          <el-button
            type="info"
            size="small"
            icon="WarningFilled"
            @click="lookSku(row)"
          ></el-button>
          <el-popconfirm
            :title="`您确定删除${row.skuName}`"
            width="250px"
            icon="delete"
            @confirm="removeSku(row)"
          >
            <template #reference>
              <el-button type="danger" size="small" icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="limit"
      background
      :page-sizes="[10, 15, 20, 30]"
      layout=" prev, pager, next, jumper,->, sizes, total,"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="getList"
    />
    <!-- 查看详情弹框 -->
    <el-drawer v-model="drawer">
      <template #header>
        <h4>查看商品详情</h4>
      </template>
      <template #default>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="item in skuInfo.skuAttrValueList"
              :key="item.id"
              style="margin: 5px 5px"
            >
              {{ item.valueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="item in skuInfo.skuSaleAttrValueList"
              :key="item.id"
              style="margin: 5px 5px"
            >
              {{ item.saleAttrValueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="100px">
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item.id"
              >
                <img :src="item.imgUrl" style="width: 130px; height: 100px" />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import {
  reqSkuList,
  reqSaleSku,
  reqCancelSale,
  reqSkuInfo,
  reqRemoveSku,
} from '@/api/product/sku'
import { SkuResponseData, SkuData, SkuInfoData } from '@/api/product/sku/type'
import { ElMessage } from 'element-plus'
const tableData = ref<SkuData[]>([])
const pageNo = ref<number>(1)
const limit = ref<number>(10)
const total = ref<number>(0)
const drawer = ref<boolean>(false)
let skuInfo = ref<any>({})

const getList = async (pager = 1) => {
  pageNo.value = pager
  const res: SkuResponseData = await reqSkuList(pageNo.value, limit.value)
  if (res.code == 200) {
    tableData.value = res.data.records
    total.value = res.data.total
  }
}
getList()
const handleSizeChange = () => {
  getList()
}

// 上架、下架
const SaleOrCancel = async (row: SkuData) => {
  // isSale==1，说明是现在是上架状态，需要更新为下架
  if (row.isSale == 1) {
    const res: SkuInfoData = await reqCancelSale(row.id as number)
    if (res.code == 200) {
      ElMessage({
        type: 'success',
        message: '下架成功',
      })
    }
    getList(pageNo.value)
  } else {
    const res = await reqSaleSku(row.id as number)
    if (res.code == 200) {
      ElMessage({
        type: 'success',
        message: '上架成功',
      })
    }
    getList()
  }
}
// 查看详情
const lookSku = async (row: SkuData) => {
  drawer.value = true
  let res: any = await reqSkuInfo(row.id as number)
  skuInfo.value = res.data
}
//
const removeSku = async (row: any) => {
  const res: any = await reqRemoveSku(row.id)
  if (res.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    getList(tableData.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: res.data,
    })
  }
}
</script>

<style scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
